import datetime
import logging
import random

import db
from config import max_task_one_minutes
from spider import Spider
import time

max_rank = 30
logging.basicConfig(level=20,
                    format='%(filename)s - %(lineno)s - %(asctime)s - %(levelname)s- %(message)s - %(funcName)s')

if __name__ == '__main__':
    while True:
        try:
            cookies = db.get_enable_cookies()
            now_time = datetime.datetime.now()
            day_begin_str = now_time.strftime("%Y-%m-%d 00:00:00")
            day_end_str = now_time.strftime("%Y-%m-%d %H:%M:%S")
            tasks = db.get_tasks("waiting", day_begin_str, day_end_str)
            logging.info(f"total tasks is {len(tasks)} {day_begin_str} {day_end_str}")
            if len(cookies) == 0:
                logging.error("cookie is null  sleep 60 seconds")
                time.sleep(60)
                continue
            cookie = cookies[random.randint(0, len(cookies) - 1)]
            for task in tasks:
                results = {"failed": [], "success": []}
                task_id = task[0]
                keyword = task[1]
                product = task[2]
                tag = f"{task[2]}:{task[1]}"
                logging.info(f"{tag} begin")
                s = Spider(task, cookie)
                d = s.process()

                if len(d) == 0:
                    results["failed"].append(task_id)
                    logging.error(f"{tag} task failed")
                else:
                    db.delete_ranks_by_task_ids([task_id])
                    if db.batch_insert_ranks(d) is True:
                        results["success"].append(task_id)
                    else:
                        results["failed"].append(task_id)
                        logging.error(f"{tag} insert failed")

                db.update_tasks("failed", results["failed"])
                db.update_tasks("success", results["success"])
                logging.info(f"{tag} end ")
                time.sleep(random.randint(60 / max_task_one_minutes, 60 / max_task_one_minutes + 5))
            logging.info("waitting for next time")
            time.sleep(60)
        except BaseException as e:
            logging.error(e)
